import { login, getProfile } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'

const state = {
  token: getToken() || '',
  profile: null,
  userdata: '1'
}

const mutations = {
  setToken(state, data) {
    setToken(data.jwt)

    state.token = data.jwt
    state.userdata = data.user
  },
  setProfile(state, data) {
    state.userdata = data
  },
  // 删除token
  removeToken(state) {
    state.token = ''
  },
  // 删除用户信息
  removeProfile(state) {
    state.profile = null
  }
}
const actions = {
  async login(store, data) {
    const res = await login(data)
    console.log('成功')
    store.commit('setToken', res)
  },

  // bug
  async getProfile(store) {
    const res = await getProfile()

    store.commit('setProfile', res)
  },

  logout(store) {
    store.commit('removeToken')
    store.commit('removeProfile')
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

